namespace ACEMPortal.UserControls
{
	using System;
	using System.Collections;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.SessionState;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.IO;

    using ScenarioFramework.Modules.DM.DataBase;
    using ScenarioFramework.Modules.DM.DataModel;
    using ScenarioFramework.Modules.DM.Application;

	/// <summary>
	///		Summary description for DocumentBlogger.
	/// </summary>
	public partial class DocumentBlogger : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.Label Label1;

		public string sIdDocument = string.Empty;
		protected DocumentManagerDBHelper dmDBHelper = null;

		XMLDocumentInstance document = null;
		private string sDocumentVersion=string.Empty;

		bool bFileExist = true;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			sDocumentVersion=(string)Session["DocumentVersion"];
			document = (XMLDocumentInstance) Session["DocumentInstance"];
            if (document == null)
                return;
			if ( Page.IsPostBack )
			{
				sIdDocument = (string)ViewState["DocumentId"];
			}
			else
			{

				sIdDocument = Request.QueryString["DocumentId"];
				ViewState["DocumentId"] = sIdDocument;

				// initialize Blog and read it into a dataset
				DataSet ds = this.ReadXmlComments();

				// if we created a new entry, append it to the BlogList
				// and save it to the xml file
				//AppendComments(ds);

				//WriteXmlComments(ds);

				// Dynamically build the blog into a WebControls.Table
				if ( bFileExist )
					RebuildTableView(ds);
			}
		}

		private DataSet ReadXmlComments()
		{
			// construct the DataSet
			DataSet ds = new DataSet();

			dmDBHelper = new DocumentManagerDBHelper();
			DocumentHelper docHelper=new DocumentHelper (dmDBHelper);

			string filename = docHelper.GetInstanceFileName(document.Id , document.XMLDocumentVersions.GetLastVersion().Id, "Blogs", "xml");

			// if the file exists, read  the blog database into the data set
			if (File.Exists(filename))
			{
				ds.ReadXml(filename);
			}
			else
			{
				bFileExist = false;
				
			}

			return ds;
		}

		void RebuildTableView(DataSet ds)
		{
			foreach (DataRow dr in ds.Tables[0].Rows)
			{
				TableRow tr = new TableRow();
				tr.Cells.Add(new TableCell());
				tr.Cells[0].ForeColor = Color.Navy;
				tr.Cells[0].Text = "<FONT SIZE=4 FACE=Rockwell><B>Commenti</B></FONT>";
				this.BlogTable.Rows.Add(tr);

                tr = new TableRow();
				tr.Cells.Add(new TableCell());
				tr.Cells[0].ForeColor = Color.Navy;
                tr.Cells[0].HorizontalAlign = HorizontalAlign.Left;
				tr.Cells[0].Text = "<FONT SIZE=3 FACE=Rockwell><B>" + dr[2].ToString() + "</B></FONT>";
				this.BlogTable.Rows.Add(tr);

				// add blog in a single column and span 2 columns
				tr = new TableRow();
				tr.Cells.Add(new TableCell());
                tr.Cells[0].HorizontalAlign = HorizontalAlign.Left;
				tr.Cells[0].Text = dr[3].ToString();
				this.BlogTable.Rows.Add(tr);

				tr = new TableRow();
				tr.VerticalAlign = VerticalAlign.Bottom;
				tr.Cells.Add(new TableCell());
				DateTime postTime = DateTime.Parse(dr[1].ToString());
				tr.Cells[0].HorizontalAlign = HorizontalAlign.Right;
				tr.Cells[0].Text = String.Format("Posted by {0},<i>{1}</i>", dr[0].ToString(),postTime.ToString("MMM dd, yyyy @ hh:mm"));
				this.BlogTable.Rows.Add(tr);
			}
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnSubmit_Click(object sender, System.EventArgs e)
		{
			DataSet ds = new DataSet();
			dmDBHelper = new DocumentManagerDBHelper();
			DocumentHelper docHelper=new DocumentHelper (dmDBHelper);

			string filename = docHelper.GetInstanceFileName(document.Id , document.XMLDocumentVersions.GetLastVersion().Id, "Blogs", "xml");

			if (File.Exists(filename))
			{
				ds.ReadXml(filename);
			}
			else
			{
				bFileExist = false;
				// otherwise we need to create a new new in-memory 
				// Database table from scratch
				DataTable theTable = new DataTable("Blogs");
				ds.Tables.Add(theTable);

				// add a name, time, title, and blog columns to our mock-up blog database
				theTable.Columns.Add("Name", Type.GetType("System.String"));
				theTable.Columns.Add("Time", Type.GetType("System.DateTime"));
				theTable.Columns.Add("Title", Type.GetType("System.String"));
				theTable.Columns.Add("Blog", Type.GetType("System.String"));

			}


			if ( ( this.txtTitle.Text != string.Empty ) && (this.txtBlog.Text != string.Empty ) )
			{
				// create a new DataRow
				DataRow dr = ds.Tables["Blogs"].NewRow();

				// Populate the row from the text boxes filled by the user
				dr[0] = Page.User.Identity.Name;
				dr[1] = DateTime.Now;
				dr[2] = this.txtTitle.Text;
				dr[3] = this.txtBlog.Text;

				// add the row to the DataSet
				ds.Tables["Blogs"].Rows.Add(dr);

				ds.WriteXml(filename);

				this.txtTitle.Text = string.Empty;
				this.txtBlog.Text = string.Empty;
			}
			
			RebuildTableView(ds);
		}
	}
}
